<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib  prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib  prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="redis" uri="http://www.shopjsp.com/redisTag/functions" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="srcPath" value="${redis:getSysConfig('sysFileVisitRoot_back')}"/>
<c:set var="basePath" value="${redis:getSysConfig('base_path_back')}"/>
<c:set var="uploadPath" value="${redis:getSysConfig('uploadFileVisitRoot')}"/>
<c:set var="htmlPath" value="${redis:getSysConfig('in_path_html')}"/>
<c:set var="defaultImg" value="${redis:getSysConfig('website_default_img')}"/>
<script type="text/javascript">
	/*******************更改为发货开始******************************/
	$(document).ready(function() {
		if($("#form_cityCourierSendGoods").length>0){ /**判断当前页面是否有填写表单并检验**/
		validateCityCourierForm();
			/**Modal验证销毁重构**/
			$('#sendGoodsModal').on('hidden.bs.modal', function() {
				reValidateForm1();
				reValidateCityCourierForm();
			});
		}
	});

	/**
	 * 重置表单验证
	 */
	function reValidateForm1(){
		$("#form1").data('bootstrapValidator', null);
		validateForm();
	}
	function reValidateCityCourierForm(){
		$("#form_cityCourierSendGoods").data('bootstrapValidator', null);
		validateCityCourierForm();
	}

	/**
	 * 指定表单字段重新进行验证
	 */
	function reValidateField(validateField){
		$("#form_cityCourierSendGoods").bootstrapValidator('revalidateField', validateField);
	}

	/**
	 * 自定义验证规则
	 */
	function validateForm(){
		form.bootstrapValidator({
			message: "无效的值",
			feedbackIcons: {
				valid: "glyphicon glyphicon-ok",
				invalid: "glyphicon glyphicon-remove",
				validating: "glyphicon glyphicon-refresh"
			},
			fields: {
				deliverySn: {
					validators: {
						notEmpty: {message: "物流单号不可为空"},
						numeric: {message: "只能输入数字"}
					}
				},
				logisticsId: {
					validators: {
						notEmpty: {message: "物流公司不可为空"}
					}
				}
			}
		});
	}

	function validateCityCourierForm(){
		$("#form_cityCourierSendGoods").bootstrapValidator({
			message: "无效的值",
			feedbackIcons: {
				valid: "glyphicon glyphicon-ok",
				invalid: "glyphicon glyphicon-remove",
				validating: "glyphicon glyphicon-refresh"
			},
			fields: {
				province: {
					validators: {
						notEmpty: {message: "省不可为空"}
					}
				},
				cities: {
					validators: {
						notEmpty: {message: "地级市不可为空"}
					}
				},
				district: {
					validators: {
						notEmpty: {message: "区(县)不可为空"}
					}
				},
				cityCourierId: {
					validators: {
						notEmpty: {message: "快递信息不可为空"}
					}
				}
			}
		});
	}

	function submitFormCityCourier() {
		/** 开启验证**/
		$("#form_cityCourierSendGoods").data('bootstrapValidator').validate();
		isValid=$("#form_cityCourierSendGoods").data('bootstrapValidator').isValid();
		if(isValid) {
			/** 验证通过后提交表单**/
			var options = {
				url: $("#form_cityCourierSendGoods").attr("action"),
				type: "post",
				dataType: "json",
				success: function (data) {
					if (data.success == true) {
						/** 先重置查询框的值**/
						resetQueryForm();
						/** 重新刷新列表数据**/
						$("#tt").bootstrapTable('refresh');
						/** 自动关闭弹出模态框窗口**/
						$(".close").click();
						showMsg("保存成功！");
					} else {
						showMsg("保存失败！");
					}
				}
			};
			$("#form_cityCourierSendGoods").ajaxSubmit(options);
		}
	}

	/**更改为发货**/
	function changeSend(ordersId,type){
		if(type==1){
			$("#type1").css("display","");
			$("#type2").css("display","none");
		}else if(type==2){
			$("#type1").css("display","none");
			$("#type2").css("display","");
		}
		$("#type2_1").css("display","none");
		$("#detailWin2").css("display","none");
		$("#view_Order_Products").css("display","none");
		$("#sendGoodsWin").css("display","");
		$("#detailWin").css("display","none");
		/**清空同城物流table信息 **/
		$("#province").val("");
		$("#cities").val("");
		$("#district").val("");
		$("#cityCourierId").val("");
		/**清空同城物流table信息 **/
		/**清空物流公司table信息 **/
		$("#deliverySn").val("");
		$(".combo-text").val("");
		/**清空物流公司table信息 **/
		$("#codeError").val("");
		createWindow(800,"auto","&nbsp;&nbsp;设置订单发货","icon-edit",false,"sendGoodsWin",10);
		$("#ordersId").val(ordersId);
		$("#logisticsType").val(type);
	}

	/**选择快递公司的同时，回填网址信息**/
	var urlInfo=function(){
		/**获取select控件的指**/
		var svalue=$("#sdeliveryCorpName").val();
		/**与数据字典中快递公司list做比较**/
		<c:forEach items="${redis:getBasicKeyBook('expressCompany')}" var="kb">
			if(svalue=="${kb.name}"){
				$("#deliveryUrl").val("${kb.value}");
			}
		</c:forEach>
	};

	/**初始化物流公司信息**/
	$(function(){
		$.ajax({
			type:"POST",
			dataType:"JSON",
			url:"${basePath}/back/logistics/initLogistics.sjson",
			data: {},
			success: function(data){
				if(data.logisticsList!=null){
					var htmlOption="<option value=''>--请选择--</option>";
					for(var i=0;i<data.logisticsList.length;i++){
						htmlOption+="<option  value='" + data.logisticsList[i].logisticsId+"'>" + data.logisticsList[i].deliveryCorpName+ "</option>";
					}
					$("#code").html(htmlOption);
				}
			}
		});
	});

	/** 同城快递地址 **/
	function changeArea(id,level){
		var province = "province";
		var city="cities";
		var district= "district";
		$.ajax({
			url:"${basePath}/back/orders/findCityList.sjson",
			type:"post",
			dataType:"json",
			data:{areaId:id},
			success:function(result){
				var areaList = result.cityList;
				if(areaList!=""){
					var htmlOption="<option value=''>--请选择--</option>";
					for(var i=0;i<areaList.length;i++){
						htmlOption+="<option  value='" + areaList[i].areaId+"'>" + areaList[i].name+ "</option>";
					}
					$("#"+id).empty().html(htmlOption);
					if(level==1){
						$("#" + city).html(htmlOption);
						$("#" + district).html("<option value=''>--请选择--</option>");
						reValidateField("cities");
						reValidateField("district");
					}else if(level==2){
						$("#" + district).html(htmlOption);
						reValidateField("district");
					}
				}
			}
		});
	}

	/** 快递信息 **/
	function changeAreaWithCityCourier(){
		var province=$("#province").val();
		var city=$("#cities").val();
		var district=$("#district").val();
		var responsibleAreas=province+","+city+","+district;
		$.ajax({
			url:"${basePath}/back/cityCourier/initCityCourier.sjson",
			type:"post",
			dataType:"json",
			data:{id : responsibleAreas},
			success:function(result){
                $("#cityCourierId").empty();
				var cityCourierList=result.cityCourierList;
				if(cityCourierList!=""){
					var htmlOption="<option value=''>--请选择--</option>";
					for(var i=0;i<cityCourierList.length;i++){
						htmlOption+="<option  value='" + cityCourierList[i].cityCourierId+"'>" + cityCourierList[i].cityCourierName+","+cityCourierList[i].responsibleAreas+","+cityCourierList[i].phone+ "</option>";
					}
					$("#cityCourierId").html(htmlOption);
				}
				reValidateField("cityCourierId");
			}
		});
	}

	/*******************更改为发货结束******************************/
</script>
<div class="modal fade" id="sendGoodsModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
	<div class="modal-dialog" style="width:70%">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button"  class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h5 class="modal-title" id="mySendGoodsModalLabel"></h5>
			</div>
			<div id="type1">
				<div class="modal-body">
					<form id="form1" method="post" action="${basePath}/back/orders/changeShipments.sjson">
						<input type="hidden" id="ordersId_shipping" name="ordersId" value=""/>
						<input type="hidden" id="logisticsType_shipping" name="logisticsType" value="1"/>
						<table align="center" class="addOrEditTable">
							<tr class="titlebg" style="text-align: center;">
								<td colspan="4">物流公司配送信息</td>
							</tr>
							<tr>
								<td class="toright_td" width="150px">物流单号:</td>
								<td  class="toleft_td">
									<div class="form-group">
										<input type="text" class="form-control input-sm" id="deliverySn" name="deliverySn" placeholder="请输入物流单号"/>
									</div>
								</td>
							</tr>
							<tr>
								<td class="toright_td" width="150px">物流公司名称:</td>
								<td>
									<div class="form-group">
										<select id="code" name="logisticsId" class="form-control input-sm" ></select>
										<span id="codeError" style=""></span>
									</div>
								</td>
							</tr>
						</table>
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" id="shippingBtn" class="btn btn-primary"  onclick="submitForm()">保存</button>
					<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
				</div>
			</div>
			<div id="type2">
				<div class="modal-body">
					<form id="form_cityCourierSendGoods" method="post" action="${basePath}/back/orders/changeShipments.sjson">
						<input type="hidden" id="ordersId_cityCourier" name="ordersId" value=""/>
						<input type="hidden" id="logisticsType_cityCourier" name="logisticsType" value="2"/>
						<table align="center" class="addOrEditTable">
							<tr class="titlebg" style="text-align: center;">
								<td colspan="4">同城快递配送信息</td>
							</tr>
							<tr>
								<td class="toright_td" width="150px">区域:</td>
								<td class="toleft_td" style="width:220px;border-right: none;">
									<div class="form-group">
										<select id="province" name="province" onchange="changeArea(this.value,'1')" class="form-control input-sm">
											<option value="">省</option>
											<c:forEach items="${provinceList}" var="first">
												<option  value="${first.areaId}">${first.name}</option>
											</c:forEach>
										</select>
									</div>
								</td>
								<td class="toleft_td" style=" width:220px;border-left: none;border-right: none;">
									<div class="form-group">
										<select id="cities" name="cities" onchange="changeArea(this.value,'2')" class="form-control input-sm">
											<option value="">地级市</option>
										</select>
									</div>
								</td>
								<td class="toleft_td" style="width:220px;border-left: none;">
									<div class="form-group">
										<select id="district" name="district" onchange="changeAreaWithCityCourier()" class="form-control input-sm">
											<option value="">区(县)</option>
										</select>
									</div>
								</td>
							</tr>
							<tr id="type2_1">
								<td class="toright_td" width="150px">快递信息:</td>
								<td colspan="3">
									<div class="form-group">
										<select id="cityCourierId" name="cityCourierId" class="form-control input-sm"></select>
									</div>
								</td>
							</tr>
						</table>
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" id="cityCourierBtn" class="btn btn-primary"  onclick="submitFormCityCourier()">保存</button>
					<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
				</div>
			</div>
		</div>
	</div>
</div>
</div>